//
//  DSHCBcakgroundWindow.m
//  kangarooOnlineStore
//
//  Created by 罗德良 on 2017/10/13.
//  Copyright © 2017年 John. All rights reserved.
//

#import "DSHCBcakgroundWindow.h"
#import "Masonry.h"
#define kScreenWidth ([UIScreen mainScreen].bounds.size.width)
#define kScreenHeight ([UIScreen mainScreen].bounds.size.height)

@interface DSHCBcakgroundWindow ()

@property (strong, nonatomic) UIView *sub;

@property (strong, nonatomic) UIButton *tapButton;

@end

@implementation DSHCBcakgroundWindow

- (instancetype)init
{
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        [self configurView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self configurView];
    }
    return self;
}

- (void)showPopWindow
{
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    self.windowLevel = UIWindowLevelAlert - 100;
    [self makeKeyAndVisible];
    self.tapHidden = YES;
}

- (void)configurView
{
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    self.windowLevel = UIWindowLevelAlert - 100;
    [self makeKeyAndVisible];
    self.tapHidden = YES;
}

- (void)hideAction:(UIButton *)sender
{
    [self endEditing:YES];
    [self hidBackgroundWindow];
}

+ (instancetype)setupBackgroundWindowAndShow
{
    return [[self alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
}

+ (instancetype)setupBackgroundWindowAndShowSmall
{
    return [[self alloc]initWithFrame:CGRectMake(0, 108, kScreenWidth, kScreenHeight)];
}


- (void)hidBackgroundWindow
{
    [self resignKeyWindow];
    self.hidden = YES;
}

- (void)addUserCustomView:(UIView *)sub
{
    [self addSubview:sub];
    _sub = sub;
    [sub mas_makeConstraints:^(MASConstraintMaker *make) {
        //
        make.edges.mas_equalTo(UIEdgeInsetsMake(0.277 * kScreenHeight, 0.12 * kScreenWidth, 0.26 * kScreenHeight, 0.12 * kScreenWidth));
    }];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHiden:) name:UIKeyboardWillHideNotification object:nil];
    [self makeKeyAndVisible];
}

- (void)addCustomView:(UIView *)sub
{
    [self addSubview:sub];
    _sub = sub;
    [sub mas_makeConstraints:^(MASConstraintMaker *make) {
        //
        make.edges.mas_equalTo(UIEdgeInsetsMake(0.277 * kScreenHeight, 0.05 * kScreenWidth, 0.26 * kScreenHeight, 0.05 * kScreenWidth));
    }];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHiden:) name:UIKeyboardWillHideNotification object:nil];
    [self makeKeyAndVisible];
}



- (void)keyboardWillShow:(NSNotification *)noti
{
    [UIView animateWithDuration:0.25 animations:^{
        //
        [_sub mas_updateConstraints:^(MASConstraintMaker *make) {
            //
            make.edges.mas_equalTo(UIEdgeInsetsMake(0.277 * kScreenHeight - 100, 0.12 * kScreenWidth, 0.26 * kScreenHeight + 100, 0.12 * kScreenWidth));
        }];
    }];
    
}

- (void)keyboardWillHiden:(NSNotification *)noti
{
    [UIView animateWithDuration:0.25 animations:^{
        //
        [_sub mas_updateConstraints:^(MASConstraintMaker *make) {
            //
            make.edges.mas_equalTo(UIEdgeInsetsMake(0.277 * kScreenHeight, 0.12 * kScreenWidth, 0.26 * kScreenHeight, 0.12 * kScreenWidth));
        }];
    }];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (UIButton *)tapButton
{
    if (!_tapButton) {
        _tapButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _tapButton.frame = self.bounds;
        [_tapButton addTarget:self action:@selector(hideAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_tapButton];
    }
    return _tapButton;
}

- (void)setTapHidden:(BOOL)tapHidden
{
    _tapHidden = tapHidden;
    if (_tapHidden) {
        [self tapButton];
    }else{
        [_tapButton removeFromSuperview];
        _tapButton = nil;
    }
}

#pragma mark -- 弹出放大缩小动画
- (void)shakeToShow:(UIView*)aView{
    [self addSubview:aView];
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.5;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    [aView.layer addAnimation:animation forKey:nil];
}



@end
